﻿using System;
using System.Web.UI.WebControls;

namespace ECommerce.Administration.Controls
{
	public partial class LanguageList : AdminBaseUsercontrol
	{
		private Service.ILanguageService _LanguageService;

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				BindLanguagesData();
			} 
		}

		protected void GridLanguages_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
		{
			if(e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.Header)
			{
				e.Row.Cells[1].Text = GetLocalResourceString("Admin.LanguageList.LanguageNameHeader");
				e.Row.Cells[2].Text = GetLocalResourceString("Admin.LanguageList.LanguageCultrueHeader");
				e.Row.Cells[3].Text = GetLocalResourceString("Admin.LanguageList.LanguagePublishedHeader");
			}
		}

		private void BindLanguagesData()
		{
			if (_LanguageService == null)
			{
				_LanguageService = new Service.LanguageService();
			}

			var languages = _LanguageService.GetAllLanguage();

			GridLanguages.DataSource = languages;
			GridLanguages.DataBind();
		}

		public void DeleteSelectedLanguage()
		{
			if (_LanguageService == null)
			{
				_LanguageService = new Service.LanguageService();
			}

			foreach(System.Web.UI.WebControls.GridViewRow rowItem in GridLanguages.Rows)
			{
				var chBox = rowItem.FindControl("cbLanguage") as CheckBox;
				var hfLanguageID = rowItem.FindControl("hfLanguageId") as HiddenField;

				bool isChecked = chBox.Checked;
				int languageID = int.Parse(hfLanguageID.Value);

				if(isChecked)
				{
					_LanguageService.DeleteLanguage(languageID);
				}
			}

			BindLanguagesData();
		}
	}
}